from PyQt5.QtWidgets import QWidget, QGridLayout, QGroupBox, QLabel, QSpinBox, QPushButton
import re


class AttSpinBox(QSpinBox):
    def __init__(self):
        super().__init__()
        self.setRange(0, 300)


class SetupAttestatFaceSizes(QWidget):

    def save_settings(self):
        lines = open('templates/attestat-face-template.html', 'r').readlines()
        for spinbox in self.info_about_sizes:
            comment = self.info_about_sizes[spinbox]
            for i in range(len(lines)):
                if comment in lines[i]:
                    value = re.findall(r'\d+', lines[i])[0]
                    lines[i] = lines[i].replace(value, str(spinbox.value()))
        with open('templates/attestat-face-template.html', 'w') as out:
            out.write(''.join(lines))

    def __init__(self):
        super().__init__()

        grid = QGridLayout(self)
        self.setLayout(grid)

        grid.addWidget(QLabel('Все размеры указаны в мм'), 0, 0)

        size_groupbox = QGroupBox('Размеры аттестата')
        size_groupbox_layout = QGridLayout()
        size_groupbox.setLayout(size_groupbox_layout)
        size_groupbox_layout.addWidget(QLabel('Ширина'), 0, 0)
        self.body_width_input = AttSpinBox()
        size_groupbox_layout.addWidget(self.body_width_input, 0, 1)
        size_groupbox_layout.addWidget(QLabel('Высота'), 1, 0)
        self.body_height_input = AttSpinBox()
        size_groupbox_layout.addWidget(self.body_height_input, 1, 1)

        grid.addWidget(size_groupbox, 1, 0)

        fio_groupbox = QGroupBox('ФИО')
        fio_groupbox_layout = QGridLayout()
        fio_groupbox.setLayout(fio_groupbox_layout)
        fio_groupbox_layout.addWidget(QLabel('Ширина'), 0, 0)
        self.fio_width_input = AttSpinBox()
        fio_groupbox_layout.addWidget(self.fio_width_input, 0, 1)
        fio_groupbox_layout.addWidget(QLabel('Отступ справа'), 1, 0)
        self.fio_right_input = AttSpinBox()
        fio_groupbox_layout.addWidget(self.fio_right_input, 1, 1)
        fio_groupbox_layout.addWidget(QLabel('Отступ сверху'), 2, 0)
        self.fio_top_input = AttSpinBox()
        fio_groupbox_layout.addWidget(self.fio_top_input, 2, 1)

        grid.addWidget(fio_groupbox, 2, 0)

        year_groupbox = QGroupBox('Год окончания')
        year_groupbox_layout = QGridLayout()
        year_groupbox.setLayout(year_groupbox_layout)
        year_groupbox_layout.addWidget(QLabel('Ширина'), 0, 0)
        self.year_width_input = AttSpinBox()
        year_groupbox_layout.addWidget(self.year_width_input, 0, 1)
        year_groupbox_layout.addWidget(QLabel('Высота'), 1, 0)
        self.year_height_input = AttSpinBox()
        year_groupbox_layout.addWidget(self.year_height_input, 1, 1)
        year_groupbox_layout.addWidget(QLabel('Отступ слева'), 2, 0)
        self.year_left_input = AttSpinBox()
        year_groupbox_layout.addWidget(self.year_left_input, 2, 1)
        year_groupbox_layout.addWidget(QLabel('Отступ сверху'), 3, 0)
        self.year_top_input = AttSpinBox()
        year_groupbox_layout.addWidget(self.year_top_input, 3, 1)

        grid.addWidget(year_groupbox, 3, 0)

        school_groupbox = QGroupBox('Учебное заведение')
        school_groupbox_layout = QGridLayout()
        school_groupbox.setLayout(school_groupbox_layout)
        school_groupbox_layout.addWidget(QLabel('Ширина'), 0, 0)
        self.school_width_input = AttSpinBox()
        school_groupbox_layout.addWidget(self.school_width_input, 0, 1)
        school_groupbox_layout.addWidget(QLabel('Отступ справа'), 1, 0)
        self.school_right_input = AttSpinBox()
        school_groupbox_layout.addWidget(self.school_right_input, 1, 1)
        school_groupbox_layout.addWidget(QLabel('Отступ сверху'), 2, 0)
        self.school_top_input = AttSpinBox()
        school_groupbox_layout.addWidget(self.school_top_input, 2, 1)

        grid.addWidget(school_groupbox, 4, 0)

        director_groupbox = QGroupBox('Руководитель организации')
        director_groupbox_layout = QGridLayout()
        director_groupbox.setLayout(director_groupbox_layout)
        director_groupbox_layout.addWidget(QLabel('Отступ слева'), 0, 0)
        self.director_left_input = AttSpinBox()
        director_groupbox_layout.addWidget(self.director_left_input, 0, 1)
        director_groupbox_layout.addWidget(QLabel('Отступ сверху'), 1, 0)
        self.director_top_input = AttSpinBox()
        director_groupbox_layout.addWidget(self.director_top_input, 1, 1)

        grid.addWidget(director_groupbox, 5, 0)

        date_groupbox = QGroupBox('Дата выдачи')
        date_groupbox_layout = QGridLayout()
        date_groupbox.setLayout(date_groupbox_layout)
        date_groupbox_layout.addWidget(QLabel('Отступ слева'), 0, 0)
        self.date_left_input = AttSpinBox()
        date_groupbox_layout.addWidget(self.date_left_input, 0, 1)
        date_groupbox_layout.addWidget(QLabel('Отступ сверху'), 1, 0)
        self.date_top_input = AttSpinBox()
        date_groupbox_layout.addWidget(self.date_top_input, 1, 1)
        date_groupbox_layout.addWidget(QLabel('Высота'), 2, 0)
        self.date_height_input = AttSpinBox()
        date_groupbox_layout.addWidget(self.date_height_input, 2, 1)

        grid.addWidget(date_groupbox, 6, 0)

        qr_groupbox = QGroupBox('QR-код')
        qr_groupbox_layout = QGridLayout()
        qr_groupbox.setLayout(qr_groupbox_layout)
        qr_groupbox_layout.addWidget(QLabel('Ширина'), 0, 0)
        self.qr_width_input = AttSpinBox()
        qr_groupbox_layout.addWidget(self.qr_width_input, 0, 1)
        qr_groupbox_layout.addWidget(QLabel('Высота'), 1, 0)
        self.qr_height_input = AttSpinBox()
        qr_groupbox_layout.addWidget(self.qr_height_input, 1, 1)
        qr_groupbox_layout.addWidget(QLabel('Отступ слева'), 2, 0)
        self.qr_left_input = AttSpinBox()
        qr_groupbox_layout.addWidget(self.qr_left_input, 2, 1)
        qr_groupbox_layout.addWidget(QLabel('Отступ сверху'), 3, 0)
        self.qr_top_input = AttSpinBox()
        qr_groupbox_layout.addWidget(self.qr_top_input, 3, 1)

        grid.addWidget(qr_groupbox, 7, 0)

        self.info_about_sizes = {
            self.body_width_input: 'body-width',
            self.body_height_input: 'body-height',
            self.fio_right_input: 'name-right',
            self.fio_width_input: 'name-width',
            self.fio_top_input: 'name-top',
            self.year_top_input: 'year-top',
            self.year_left_input: 'year-left',
            self.year_height_input: 'year-height',
            self.year_width_input: 'year-width',
            self.school_right_input: 'school-right',
            self.school_width_input: 'school-width',
            self.school_top_input: 'school-top',
            self.director_left_input: 'director-left',
            self.director_top_input: 'director-top',
            self.date_top_input: 'date-top',
            self.date_height_input: 'date-height',
            self.date_left_input: 'date-left',
            self.qr_left_input: 'qr-left',
            self.qr_top_input: 'qr-top',
            self.qr_width_input: 'qr-width',
            self.qr_height_input: 'qr-height'
        }

        inp = open('templates/attestat-face-template.html', 'r')

        for spinbox in self.info_about_sizes:
            comment = self.info_about_sizes[spinbox]
            inp.seek(0)
            for line in inp:
                if comment in line:
                    value = int(re.findall(r'\d+', line)[0])
                    spinbox.setValue(value)

        save_button = QPushButton('Сохранить настройки')
        save_button.clicked.connect(self.save_settings)
        grid.addWidget(save_button)

        self.setMinimumWidth(500)
        self.setMinimumHeight(400)
        self.setWindowTitle('Настройка размеров лицевой стороны аттестата')
        self.show()
